<?php
namespace Extend\Controller;

/**
 * 会员RPC接口
 * @modify wenhui 2018-01-17 创建
 */
class CustomerController extends ParentController {
    protected function _initialize() {
        $this->validateUtil = new \Org\Util\Validate();
    }

    /**
     * 获取用户个人会员卡信息
     * @modify wenhui 2018-01-17 创建
     */
    public function getVipCardDetial($data) {
        if (empty(parent::checkRPCRight($data))) {
            return parent::returnJson(403, 202);
        }
        $validateRule = array(
            'hotelId' => 'notnull,notempty,id',
            'vipInfoId' => 'notnull,notempty,id',
            'vipUserId' => 'notnull,notempty,id',
        );
        $validatePass = $this->validateUtil->validate($data, $validateRule);
        if (empty($validatePass)) {
            $message = $this->validateUtil->getError('explode', ',');
            return parent::returnJson($message['msg'][0], $message['msg'][1]);
        }

        $hotelId = $data['hotelId'];
        $vipInfoId = $data['vipInfoId'];
        $vipUserId = $data['vipUserId'];

        // 客栈是否共享
        $vipInitModel = D('VipInit');
        $adminHotelModel = D('AdminHotel');
        $hotelCreator = $adminHotelModel->getHotelCreator($hotelId);
        $vipInitInfo = $vipInitModel->getInfoByAdmin($hotelCreator['adminlogin_id']);
        $allHotel = $vipInitInfo['share'] == 1 ? 1 : null;
        
        $customerLogic = A('Common/Customer', 'Logic');
        $customerInfos = $customerLogic->getVipInfos($hotelId, null, $vipInfoId, $allHotel, null, null, null);
        $customerInfos['item'] = getArrayValueAsIndex($customerInfos['item'], 'id');
        // 处理数据
        $vipInfoModel = D('VipInfo');
        $vipInfo = $vipInfoModel->getInfoByHotel($hotelId);

        $customerInfos['item'][$vipUserId]['hotelVipInfo'] = $vipInfo;
        $data = $customerInfos['item'][$vipUserId];

        return parent::returnJson(200, 200, $data);
    }

    /**
     * 获取会员卡记录
     * @modify wenhui 2017-12-06    创建
     */
    public function getUserVipCardRecord($data){
        if (empty(parent::checkRPCRight($data))) {
            return parent::returnJson(403, 202);
        }

        $validateRule = array(
            'hotelId' => 'notnull,notempty,id',
            'vipUserId' => 'notempty,id',
        );
        $validatePass = $this->validateUtil->validate($data, $validateRule);
        if (empty($validatePass)) {
            $message = $this->validateUtil->getError('explode', ',');
            return parent::returnJson($message['msg'][0], $message['msg'][1]);
        }

        $hotelId = $data['hotelId'];
        $vipUserId = $data['vipUserId'];
        $langSet = $data['langSet'];

        // 校验客栈是否过期,是否禁用
        $hotelLogic = A('Common/Hotel', 'Logic');
        $result = $hotelLogic->checkHotelActive($hotelId);
        if ($result == false) {
            return parent::returnJson(403, 408);
        }
        $result = $hotelLogic->checkHotelStatus($hotelId);
        if ($result == false) {
            return parent::returnJson(403, 401);
        }

        $customerLogic = A('Common/Customer', 'Logic');
        $vipCardRecordInfos = $customerLogic->getVipCardRecord($hotelId, $vipUserId, $langSet);

        return parent::returnJson(200, 200, $vipCardRecordInfos);
    }
}